import UserDao, { Userinfo } from '../dao/UserDao'
import jwt from 'jsonwebtoken'
export const ACCESS_TOKEN_SECRET = 'youraccesstokensecret'

class UserInfoService {
  static userInfoService: UserInfoService = new UserInfoService()
  async login(username: string, pws: string) {
    const userInfo = await UserDao.findOneUser(username, pws)
    if (userInfo) this.createJWTToken(userInfo as Userinfo)
    return userInfo
  }
  createJWTToken(userInfo: Userinfo) {
    const token: string = jwt.sign({ data: userInfo }, ACCESS_TOKEN_SECRET, { expiresIn: '180h', header: { alg: 'HS256', typ: 'JWT' } })
    userInfo.token = token
  }
}

export default UserInfoService.userInfoService
